﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System;
using System.Linq;
using System.Collections.Specialized;
using System.ComponentModel;
using System.IO;
using AForge.Imaging;
using BrainTechLLC;
using BrainTechLLC.FilterExtensions;
using ImageAppDataModels;
using ImageAppViewModels;
using System.Collections.Generic;
using BrainTechLLC.ThreadSafeObjects;

namespace WpfImageDuplicates
{
    public class SettingsWatcher : ISettingsWatcher
    {
        protected Window1 _parentWindow;

        public Settings _settings;
        public SettingsViewModel SettingsViewModel { get; set; }

        public dynamic SettingsViewModelDynamic
        {
            get
            {
                dynamic d = SettingsViewModel;
                return d;
            }
        }

        public SettingsWatcher(Window1 parentWindow)
        {
            _parentWindow = parentWindow;
        }

        public void SaveData()
        {
            SettingsViewModel.SaveSettings(FilePaths._settingsPath);
        }

        public void LoadSettings()
        {
            _settings = Settings.LoadSettings(FilePaths._settingsPath);

            if (_settings == null)
                _settings = new Settings();

            Settings.Values = _settings;
            SettingsViewModel = new SettingsViewModel(Settings.Values);
            HSL.InitializeHSL(_settings);
            _settings.PropertyChanged += new PropertyChangedEventHandler(_settings_PropertyChanged);
        }

        void _settings_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "ThumbHeight" ||
                e.PropertyName == "ThumbWidth" ||
                e.PropertyName == "StandardColorPixelFormat" ||
                e.PropertyName == "WantFilterThumbnails" ||
                e.PropertyName == "HighQualityThumbs" ||
                e.PropertyName == "InterpolationMode" ||
                e.PropertyName == "DecimalDigitsHSLSaturationRounding" ||
                e.PropertyName == "DecimalDigitsHSLLuminanceRounding" ||
                e.PropertyName == "ShowImageInfoBelowThumbs" ||
                e.PropertyName == "UseHSLRounding" ||
                e.PropertyName == "SmoothingMode")
            {
                _parentWindow.FilterHandler.OnFiltersGenerationsChanges(false, true);
            }
            else if (e.PropertyName == "ShowImageInfoBelowThumbs" ||
                     e.PropertyName == "EvenThumbWidths" ||
                     e.PropertyName == "ShowThumbCommandBar")
            {
                _parentWindow.MarkThumbnailFileNameLocationChanged();
            }
            else if (e.PropertyName == "ItemsToTossWhenMaxReached" ||
                     e.PropertyName == "MaxBitmapsInMemory")
            {
                RecentlyUsedFileImages.RecentlyUsedBitmaps.MaxItems = Settings.Values.MaxBitmapsInMemory;
                RecentlyUsedFileImages.RecentlyUsedBitmaps.ItemsToTossWhenMaxReached = Settings.Values.ItemsToTossWhenMaxReached;
            }
            else if (e.PropertyName == "TransformedToTossWhenMaxReached" ||
                 e.PropertyName == "MaxTransformedBitmapsInMemory")
            {
                RecentlyUsedFileImages.RecentlyUsedTransformedBitmaps.MaxItems = Settings.Values.MaxTransformedBitmapsInMemory;
                RecentlyUsedFileImages.RecentlyUsedTransformedBitmaps.ItemsToTossWhenMaxReached = Settings.Values.TransformedToTossWhenMaxReached;
            }
            else if (e.PropertyName == "ImageSourcesToTossWhenMaxReached" ||
                     e.PropertyName == "MaxImageSourcesInMemory")
            {
                RecentlyUsedFileImages.RecentlyUsedImageSources.MaxItems = Settings.Values.MaxBitmapsInMemory;
                RecentlyUsedFileImages.RecentlyUsedImageSources.ItemsToTossWhenMaxReached = Settings.Values.ItemsToTossWhenMaxReached;
            }
            else if (e.PropertyName == "FileBytesToTossWhenMaxReached" ||
                     e.PropertyName == "MaxFileBytesInMemory")
            {
                RecentlyUsedFileImages.RecentlyUsedFileBytes.MaxItems = Settings.Values.MaxFileBytesInMemory;
                RecentlyUsedFileImages.RecentlyUsedFileBytes.ItemsToTossWhenMaxReached = Settings.Values.FileBytesToTossWhenMaxReached;
            }
            else if (e.PropertyName == "FileNameFilter" || e.PropertyName == "MinSizeFilter" || e.PropertyName == "MaxSizeFilter")
            {
                HandleFileNameFilter();
            }
        }

        private void HandleFileNameFilter()
        {
            // should lock this in a crit section

            if (Window1.MainWindow._originalFileImageViewModels == null)
            {
                Window1.MainWindow._originalFileImageViewModels = new ThreadSafeList<FileImageViewModel>();
                Window1.MainWindow.FileImageCollectionViewModels.ForEach(o => Window1.MainWindow._originalFileImageViewModels.Add(o.ToList()));
            }

            List<FileImageViewModel> meetsCriteria = new List<FileImageViewModel>();
            var groups = Window1.MainWindow.AllGroups.Where(o => o.IsSelected).ToList();
            if (groups.Count == 0)
            {
                groups = Window1.MainWindow.AllGroups.ToList();
            }

            for (int n = 0; n < Window1.MainWindow._originalFileImageViewModels.Count; n++)
            {
                var vm = Window1.MainWindow._originalFileImageViewModels[n];
                if (vm.MeetsFilterCriteria(SettingsViewModelDynamic.FileNameFilter, SettingsViewModelDynamic.MinSizeFilter, SettingsViewModelDynamic.MaxSizeFilter, groups))
                {
                    meetsCriteria.Add(vm);
                }
            }

            Window1.MainWindow.CollectionViews.ForEach(o => o.Filter(meetsCriteria));
        }

    }
}
